﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BugTracker.BTGridView;
using BugTracker.BusinessEntities.Entities.Company;
using BugTracker.BusinessEntities.Entities.Project;
using BugTracker.BusinessLogic.BTUser;
using BugTracker.BusinessLogic.Company;
using BugTracker.BusinessLogic.Project;
using BugTracker.Core.Infrastructure.Exceptions;
using BugTracker.WebUX.WebCode;
using Microsoft.Practices.Unity;
using BugTracker.BusinessLogic.Project.ProjectDetail;
using Microsoft.Reporting.WebForms;
using BugTracker.WebUX.Workspace.RDLC.Design;
using System.Data;
using BugTracker.BusinessLogic.Issue.Details;

namespace BugTracker.WebUX.Workspace.RDLC.Modules
{
    public partial class UCProjectReport : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    PickReport();
                }
            }
            catch (Exception ex) {
                MessageLiteral.Text = ex.Message;
            }
        }

        private void PickReport()
        {
            var r = Request.QueryString["r"];
            if (r != null)
            {
                if (r.Equals("1"))
                {
                    ProjectDetailReport();
                }
                else if (r.Equals("2"))
                {
                    IssueDetailReport();
                }
            }
        }

        private void ProjectDetailReport()
        {
            var data = Global.IoC.Container.Resolve<IProjectDetailService>().Report();
            
            ProjectReportViewer.Reset();
            ProjectReportViewer.ProcessingMode = ProcessingMode.Local;
            ProjectReportViewer.LocalReport.ReportPath = Server.MapPath("~/Workspace/RDLC/Reports/ProjectsReport.rdlc");
            ProjectReportViewer.LocalReport.DataSources.Clear();

            var rds = new ReportDataSource(ProjectReportViewer.LocalReport.GetDataSourceNames()[0], data);

            ProjectReportViewer.LocalReport.DataSources.Add(rds);
            ProjectReportViewer.LocalReport.Refresh();
        }

        private void IssueDetailReport()
        {
            var data = Global.IoC.Container.Resolve<IIssueDetailService>().Report();

            ProjectReportViewer.Reset();
            ProjectReportViewer.ProcessingMode = ProcessingMode.Local;
            ProjectReportViewer.LocalReport.ReportPath = Server.MapPath("~/Workspace/RDLC/Reports/IssuesReport.rdlc");
            ProjectReportViewer.LocalReport.DataSources.Clear();

            var rds = new ReportDataSource(ProjectReportViewer.LocalReport.GetDataSourceNames()[0], data);

            ProjectReportViewer.LocalReport.DataSources.Add(rds);
            ProjectReportViewer.LocalReport.Refresh();
        }
    }
}